package com.laozhao.dreamframe.sqlmap;

/** 
 * @author dozhao 
 * @version create time：Aug 1, 2011 3:40:46 PM 
 * 
 */

import java.util.Date;
import java.util.List;

/**
 * SQL动态封装工具
 * 
 * simple：：
 * 
 * BaseSelector select = new BaseSelector(); select.setCommand("select * from
 * stat_user "); select.createCriteria().andEqualTo("org_code", "YZ1201")
 * .andGreaterThanOrEqualTo("day_code", "2009-01-01")
 * .andLessThanOrEqualTo("day_code", "2009-03-31"); List<Map> rowset =
 * SqlTemplate.executeQuery(select.toSql(),new Object[] {});
 * 
 * output:select * from stat_user where org_code = 'YZ1201' and day_code >=
 * '2009-01-01' and day_code <= '2009-03-31'
 * 
 */
public class BaseSelector {

	/**
	 * sql语句
	 * select XX,XX,XX from XXXX $where <oredCriteria> $order<orderByClause> 
	 */
	private String command;
	/**
	 * 排序字段
	 */
	protected String orderByClause;


	/**
	 * sql条件
	 */
	protected List<Criteria> oredCriteria;

//	public BaseSelector() {
//		command = "";
//		oredCriteria = new ArrayList<Criteria>();
//		orderByClause = "";
//	}
//
//	public BaseSelector(Criteria criteria) {
//		command = "";
//		this.oredCriteria = new ArrayList<Criteria>();
//		this.oredCriteria.add(criteria);
//		orderByClause = "";
//	}
//
//	public BaseSelector(List<Criteria> oredCriteria) {
//		command = "";
//		this.oredCriteria = oredCriteria;
//	}

	public BaseSelector(String command , List<Criteria> oredCriteria , String orderByClause) {
		this.command = command;
		this.oredCriteria = oredCriteria;
		this.orderByClause = orderByClause;
	}


	/**
	 * 生成最终sql语句
	 */
	public String toSql() {
		StringBuffer sqlSel = new StringBuffer();
		if (oredCriteria != null && oredCriteria.size()> 0){
			for (Criteria cri : oredCriteria) {
				if (!cri.isValid())
					continue;
				sqlSel.append(cri.toSql());
			}
		}
		
		command.replace("$where", sqlSel.toString());
//		if (orderByClause != null) {
			command.replace("$order", orderByClause);
//		}
//		command.replaceAll("$sysdate", new Date().toString());
		
		return command;
	}


	/**
	 * 创建条件对象
	 */
	public Criteria createCriteria(boolean logic) {
		Criteria criteria = new Criteria(logic);
		// if (oredCriteria.size() == 0) {
		oredCriteria.add(criteria);
		// }
		return criteria;
	}


	/**
	 * 清除条件
	 */
	public void clear() {
		oredCriteria.clear();
	}

	String getFieldName(String field) {

		if (field == null) {
			throw new RuntimeException(field + " cannot be null");
		}
		return field.toUpperCase();
		//
		//
		// Pattern pattern = Pattern.compile("[A-Z]{1}");
		// Matcher m = pattern.matcher(field);
		// StringBuffer sbr = new StringBuffer();
		// while(m.find())
		// m.appendReplacement(sbr, "_"+m.group());
		// m.appendTail(sbr);
		// return sbr.toString().toUpperCase();
	}


//	public void setCommand(String command) {
//		this.command = command;
//	}
//
//	public void setOrderByClause(String orderByClause) {
//		this.orderByClause = orderByClause;
//	}
//
//	public void setOredCriteria(List<Criteria> oredCriteria) {
//		this.oredCriteria = oredCriteria;
//	}
}
